package com.hqyj.controller;


import com.hqyj.config.SmsConfig;
import com.hqyj.pojo.*;
import com.hqyj.service.IUserService;
import com.tencentcloudapi.sms.v20210111.SmsClient;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author rock
 * @since 2022-09-30
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    IUserService userService;

    @Autowired
    RedisTemplate<String,Object> redisTemplate;

    //邮件发送
    @PostMapping("/sendEmailCode")
    public ResultVo sendEmailCode(String userEmail){
        return userService.sendEmailCode(userEmail);
    }

    //修改密码发送验证邮件
    @PostMapping("/pwdSendEmailCode")
    public ResultVo pwdSendEmailCode(RuleForm ruleForm){
        return userService.pwdSendEmailCode(ruleForm);
    }

    //邮件登录
    @PostMapping("/emailLogin")
    public ResultVo emailLogin(String userEmail,String code){
        return userService.emailLogin(userEmail,code);
    }

    //短信发送
    @PostMapping("/sendSmsCode")
    public ResultVo sendSmsCode(String userTel){
        return userService.sendSmsCode(userTel);
    }

    //短信登录
    @PostMapping("/smsLogin")
    public ResultVo smsLogin(String userTel,String code){
        return userService.smsLogin(userTel,code);
    }

    //用户登录
    @PostMapping("/userLogin")
    public ResultVo userLogin(String userName,String userPwd){
        User user = new User();
        user.setUserName(userName);
        user.setUserPwd(userPwd);
        try {
            //0.判断当前用户名密码是否为空
            if (user.getUserName() == "" || user.getUserPwd() == ""){
                ResultVo resultVo = new ResultVo();
                resultVo.setCode(500);
                resultVo.setInfo("用户名或密码为空");
                return resultVo;
            };
            //1.获取当前用户对象主体
            Subject subject = SecurityUtils.getSubject();
            //2.将请求参数封装在token中
            UsernamePasswordToken token = new UsernamePasswordToken(user.getUserName(),user.getUserPwd());
            //3.完成登录
            subject.login(token);
            //把登录的用户名存入到session中，并设置名为loginUserName
//            subject.getSession().setAttribute("loginUserName",user.getUserName());
        } catch (UnknownAccountException e) {
            ResultVo resultVo = new ResultVo();
            resultVo.setCode(500);
            resultVo.setInfo("用户名或密码不正确");
            return resultVo;
//            return new ResultUtils(500,"用户名不存在",null);
        } catch (IncorrectCredentialsException e){
//            return new ResultUtils(500,"密码不对",null);
            ResultVo resultVo = new ResultVo();
            resultVo.setCode(500);
            resultVo.setInfo("密码不对");
            return resultVo;
        }
        ResultVo resultVo = new ResultVo();
        resultVo.setCode(200);
        resultVo.setInfo("登录成功");
        resultVo.setData(user);
        return resultVo;
    }

//    @PostMapping("/index")
//    public ResultVo index(){
//        ResultVo resultVo = new ResultVo();
//        resultVo.setCode(200);
//        resultVo.setInfo("登录成功");
//        return resultVo;
//    }

    //退出登录
    @PostMapping("/logout")
    public ResultVo logout(){
        Subject subject = SecurityUtils.getSubject();
        subject.logout();
        redisTemplate.delete("userkey");
        ResultVo resultVo = new ResultVo();
        resultVo.setCode(200);
        resultVo.setInfo("退出登录成功");
        return resultVo;
    }

    //设置未登录的处理接口
    @GetMapping("/LoginError")
    public ResultVo userIndex(){
        ResultVo resultVo = new ResultVo();
        resultVo.setCode(500);
        resultVo.setInfo("您未登录");
        return resultVo;
    }

    //根据用户名查询权限
    @PostMapping("/index")
    public ResultVo queryByName(String name){
        return userService.queryByName(name);
    }

    //用户分页查询
    @GetMapping("/userList")
    public ResultVo userList(UserRoleVo userRoleVo){
        return userService.userList(userRoleVo);
    }

    //修改用户
    @GetMapping("/userUpdate")
    public ResultVo userUpdate(UserRoleVo userRoleVo){
        return userService.userUpdate(userRoleVo);
    }

    //删除用户
    @GetMapping("/userDelete")
    public ResultVo userDelete(UserRoleVo userRoleVo){
        return userService.userDelete(userRoleVo);
    }

    //添加用户
    @GetMapping("/userAdd")
    public ResultVo userAdd(UserRoleVo userRoleVo){
        return userService.UserRoleVo(userRoleVo);
    }

    //修改密码
    @PostMapping("/updatePwd")
    public ResultVo updatePwd(RuleForm ruleForm){
        return userService.updatePwd(ruleForm);
    }

    //查询角色权限
    @GetMapping("/powerList")
    public ResultVo powerInfo(PowerList powerList){
        return userService.powerInfo(powerList);
    }

    //修改角色权限
    @GetMapping("/powerUpdate")
    public ResultVo powerUpdate(String str,String roleId){
        return userService.powerUpdate(str,roleId);
    }
}

